package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Market;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.MarketService;
import com.woniu.market_stall.vo.MarketVo;
import com.woniu.market_stall.vo.StallVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("market")
public class MarketController {

    @Autowired
    MarketService marketService;

    @GetMapping("/list")
    @ApiOperation(value = "本接口是查询市场数据的")
    public ResultResponse<List<Market>> list(){
        ResultResponse<List<Market>> result = null;
        try{
            List<Market> market = marketService.queryAll();
            result = new ResultResponse<>(200 , "查询市场成功！" ,market);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201 , "查询市场失败！" );
        }

        return result;
    }

    @GetMapping("/marketList")
    @ApiOperation(value = "本接口是查询按条件分页查询市场数据的")
    public ResultResponse<PageInfo<MarketVo>> list(
            @ApiParam(value = "当前页的参数") @RequestParam(value = "now", defaultValue = "1") Integer now,
            @ApiParam(value = "页面大小的参数") @RequestParam(value = "size", defaultValue = "3") Integer size,
            @ApiParam(value = "按条件搜索的市场名称") @RequestParam(value = "marketName", defaultValue = "") String marketName,
            @ApiParam(value = "按条件搜索的市场状态") @RequestParam(value = "status", defaultValue = "") String status) {
        ResultResponse<PageInfo<MarketVo>> result = null;
        try {
            PageInfo<MarketVo> pageInfo = marketService.queryMarketByCons(now, size, marketName , status);
            result = new ResultResponse<>(200, "查询市场成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询市场失败!");
        }
        return result;
    }

    /**
     * 删除 市场信息
     *
     * @param id 需要删除的id
     * @return
     */
    @PutMapping("/del/{id}")
    @ApiOperation(value = "本接口是删除市场")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            int i = marketService.del(id);
            if(i == 1){
                result = new ResultResponse<>(200, "删除市场成功!");
            }else {
                result = new ResultResponse<>(202, "删除失败，开放中的市场不可删除!");
            }


        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除市场失败!");
        }
        return result;
    }


    @PostMapping("/add")
    @ApiOperation(value = "本接口是添加新的市场的")
    public ResultResponse<Void> add(@RequestBody MarketVo marketVo) {
        ResultResponse<Void> result = null;
        try {
            marketService.add(marketVo);
            result = new ResultResponse<>(200, "添加市场成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加市场失败");
        }
        return result;
    }

    /**
     * 修改 摊位信息
     *
     * @param marketVo
     * @return
     */
    @PutMapping("/modify")
    @ApiOperation(value = "本接口是修改市场")
    public ResultResponse<Void> modify(@RequestBody MarketVo marketVo) {
        ResultResponse<Void> result = null;
        try {
            marketService.modify(marketVo);
            result = new ResultResponse<>(200, "修改市场成功!");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改市场失败!");
        }
        return result;
    }

}
